<?php

class appBasic {

	/**
     * 手机端APP启动图片查询
     * @param int $id   启动图片id
     */
	public static function spalashInfo($id=''){
		/** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_spalash = M('app_spalash');
        Session::switchCustomer($customerId);

        /** 3.  数据处理 */
        $where_spalash['deleted'] = 0;
		$where_spalash['cid'] = $customerId;
        if ($id){
            $where_spalash['id'] = array("gt",$id);
        }
    	$spalashInfo = $app_spalash->where($where_spalash)->find();
        $spalashInfo = empty($spalashInfo) ? [] : $spalashInfo;

    	/** 4.  信息返回 */
		$return['code'] = 0;
        $return['msg'] = 'app启动图片列表返回';
        $return['data'] = $spalashInfo;
        Output::instance()->object($return);
    }

    /**
     * 手机端APP获取最新版本
     */
	public static function newUpgradeInfo(){
		/** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_upgrade = M('app_upgrade');
        Session::switchCustomer($customerId);
        /** 3.  数据处理 */
        $where_upgrade['deleted'] = 0;
        $where_upgrade['cid'] = $customerId; 
        $versionNumber = $app_upgrade->field('max(versionNumber)')->where($where_upgrade)->find();
		$where_upgrade['versionNumber'] = $versionNumber['max(versionNumber)'];
    	$upgradeInfo = $app_upgrade->where($where_upgrade)->find();
    	/** 4.  信息返回 */
		$return['code'] = 0;
        $return['msg'] = 'app版本信息返回';
        $return['data'] = $upgradeInfo;
        Output::instance()->object($return);
    }

     /**
     * 存日活量
     * @param int $cuid   用户id
     * @param int $IMEI   IMEI手机唯一标识
     * @param string $mobilePhoneNumber   偏移量
     * @param string $appPhoneNumber   偏移量
     * @param int $systemUseTime   系统使用时长
     * @param int $appUseTime   应用使用时长
     * @param string $day   日期
     */
	public static function addDailyLive($cuid='',$IMEI,$mobilePhoneNumber,$appPhoneNumber,$systemUseTime,$appUseTime,$day){
		/** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer(0);
        $app_dailylive = M('app_dailylive');
        Session::switchCustomer($customerId);

        /** 3.  数据处理 */
        $time =  date('Y-m-d H:i:s',time());
        $add_dailylive['cid'] = $customerId;
    	$add_dailylive['cuid'] = $cuid;
    	$add_dailylive['IMEI'] = $IMEI;
    	$add_dailylive['mobilePhoneNumber'] = $mobilePhoneNumber;
    	$add_dailylive['appPhoneNumber'] = $appPhoneNumber;
    	$add_dailylive['systemUseTime'] = $systemUseTime;
    	$add_dailylive['appUseTime'] = $appUseTime;
    	$add_dailylive['day'] = $day;
    	$add_dailylive['createTime'] = $time;
    	$add_result = $app_dailylive->add($add_dailylive);

		/** 4.  判断添加是否成功 */
    	if ($add_result !== false) {
    		$return['code'] = 0;
            $return['msg'] = '添加成功';
            $return['data'] = [];
            Output::instance()->object($return);
    	}else{
    		$return['code'] = 1;
            $return['msg'] = '添加失败请重试';
            $return['data'] = [];
            Output::instance()->object($return);
    	}
    }

    /**
     * 第三方应用app下载页
     */
    public static function downloadApp(){
       Output::instance()->module('appBasic', "downloadApp")->object();
    }

      /**
     * 存与用户对应的RegistrationID信息
     * @param int $cuid   用户id
     * @param string $cuid   用户id
     */
    public static function saveRegistrationID($uid,$registrationID,$accessToken){
        /** 1. 判断是否登录 */
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        //验证accessToken是否有效
        Module::callModule("personCenter",null,"verifyToken",$accessToken);

        /** 2. 实例化数据库 */
        $customerId = Session::$user['customerId'];
        Session::switchCustomer($customerId);
        $user = M('user');

        /** 3.  数据处理 */
        $result = $user->where("id=$uid")->setField('JPUSHRegistrationID',$registrationID);

        /** 4.  判断添加是否成功 */
        if ($result !== false) {
            $return['code'] = 0;
            $return['msg'] = '添加成功';
            Output::instance()->object($return);
        }else{
            $return['code'] = 1;
            $return['msg'] = '添加失败请重试';
            Output::instance()->object($return);
        }
    }


}


